package org.maylove.wuziqi.controller;

import org.maylove.wuziqi.common.RedisUtil;
import org.maylove.wuziqi.common.ResposeMessage;
import org.maylove.wuziqi.pojo.discuss.discuss.Discuss;
import org.maylove.wuziqi.pojo.discuss.discuss.dto.GetDiscussDto;
import org.maylove.wuziqi.pojo.discuss.discussBody.DiscussBody;
import org.maylove.wuziqi.pojo.discuss.discussBody.dto.AddDiscussBodyDto;
import org.maylove.wuziqi.service.DiscussService;
import org.maylove.wuziqi.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/discuss")
public class DiscussController {
    @Autowired
    private DiscussService discussService;
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;

    Logger logger = LoggerFactory.getLogger(getClass());

    //  发布讨论
    @PostMapping("/addDiscuss")
    @ResponseBody
    public ResposeMessage addDiscuss(@RequestHeader("token") String token, @RequestBody String discuss) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        logger.info("Player id:{} add discuss:{} success", ((Long) redisUtil.get(token + "_userId")), discuss);
        discussService.addDiscuss((Long)redisUtil.get(token + "_userId"), discuss);
        return ResposeMessage.success("success", null);
    }

    //  删除讨论
    @PostMapping("/deleteDiscuss")
    @ResponseBody
    public ResposeMessage deleteDiscuss(@RequestHeader("token") String token, @RequestBody Long discussId) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        //  判权
        int identity = userService.getIdentity((Long)redisUtil.get(token + "_userId"));
        if(identity<2 && !discussService.getDiscussUserId(discussId).equals((Long)redisUtil.get(token + "_userId"))) {
            return ResposeMessage.success("Insufficient user permissions!", null);
        }

        logger.info("Player id:{} delete discuss id:{} success", ((Long) redisUtil.get(token + "_userId")), discussId);

        discussService.deleteDiscuss(discussId);
        List<DiscussBody>bodyList=discussService.getDiscussBody(discussId);
        for(DiscussBody body:bodyList){
            discussService.deleteDiscussBody(body.getDiscussBodyId());
        }
        return ResposeMessage.success("success", null);
    }

    //  查看讨论
    @PostMapping("/getDiscuss")
    @ResponseBody
    public ResposeMessage<List<Discuss> > getDiscuss(@RequestHeader("token") String token, @RequestBody GetDiscussDto getDiscussDto) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        logger.info("Player id:{} get discuss success", ((Long) redisUtil.get(token + "_userId")));

        return ResposeMessage.success("success", discussService.getDiscuss(getDiscussDto.getPageNum(), getDiscussDto.getPageSize()));
    }

    //  添加讨论内容
    @PostMapping("/addDiscussBody")
    @ResponseBody
    public ResposeMessage addDiscussBody(@RequestHeader("token") String token, @RequestBody AddDiscussBodyDto addDiscussBodyDto) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        logger.info("Player id:{} add discuss id:{} body:{} success", ((Long) redisUtil.get(token + "_userId")), addDiscussBodyDto.getDiscussId(), addDiscussBodyDto.getContent());

        discussService.addDiscussBody((Long)redisUtil.get(token + "_userId"), addDiscussBodyDto);
        return ResposeMessage.success("success", null);
    }

    //  删除讨论内容
    @PostMapping("/deleteDiscussBody")
    @ResponseBody
    public ResposeMessage deleteDiscussBody(@RequestHeader("token") String token, @RequestBody Long discussBodyId) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        //  判权
        int identity = userService.getIdentity((Long)redisUtil.get(token + "_userId"));
        if(identity<2 && !discussService.getDiscussBodyUserId(discussBodyId).equals((Long)redisUtil.get(token + "_userId"))) {
            return ResposeMessage.success("Insufficient user permissions!", null);
        }

        logger.info("Player id:{} delete discuss body id:{} success", ((Long) redisUtil.get(token + "_userId")), discussBodyId);

        discussService.deleteDiscussBody(discussBodyId);
        return ResposeMessage.success("success", null);
    }

    //  查找讨论内容
    @PostMapping("/getDiscussBody")
    @ResponseBody
    public ResposeMessage<List<DiscussBody> > getDiscussBody(@RequestHeader("token") String token, @RequestBody Long discussId) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        logger.info("Player id:{} get discuss id:{} body success", ((Long) redisUtil.get(token + "_userId")), discussId);

        return ResposeMessage.success("success", discussService.getDiscussBody(discussId));
    }
}
